<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use app\base\model\Admin;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Common extends Controller{
    //管理员信息
    protected $adminInfo;
    //管理员模型
    protected $admin;
    //管理员id
    protected $admin_id;
    //是否是超级管理员
    protected $is_admin;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $session_id = $this->request->post('session_id','');
        if(empty($session_id)){
            $msg=checkMsg(LOGIN_ERROR);
            return ['message'=>$msg];
        }
        $this->adminInfo=Session::get($session_id,'admin');
        $this->admin_id =$this->adminInfo['id'];
       if($this->adminInfo['user_type']==1){
         $this->is_admin=1;
       }else{
           $this->is_admin=0;
       }
    }
    public function uploadVideo($video_obj){
        require_once APP_PATH . '/../vendor/qiniu/autoload.php';
        $access_key = 'KZX1JQjxufw4_PYt2XEWA3nLud7c1fTwjcEJR96n';
        $secret_key = 'P5v8lyYMIIz7s28TAfGcS2xFTgd5ltfpd87R34LQ';
        $auth = new Auth($access_key,$secret_key);
        $bucket='tiance_video';//存储空间
        $token = $auth->uploadToken($bucket);
        $uploadMgr = new UploadManager();
        $filePath=$video_obj->getRealPath();
        $ext = pathinfo($video_obj->getInfo('name'), PATHINFO_EXTENSION);  //后缀
        $key =substr(md5($video_obj->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
        $domain ='psmcstic6.bkt.clouddn.com';
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
// 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err)  = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
           return ['code'=>0];
        } else {
            $video_url='http://'.$domain.'/'.$key;
            return ['code'=>1,'video_url'=>$video_url];
        }
    }
}